function Jazzila(){
this.base=Listener;
this.base();
this.imPrefs = new imPrefs();
var isConfigured =  this.imPrefs.getBoolPref(this.imPrefs.PREF_DEBUG_CONSOLE_ENABLED);
  if (isConfigured == null)
  {
     // set prefs if they don't exit
	this.initPref(this.imPrefs.PREF_STATUSBAR_ENABLED , "bool", true);
 	this.initPref(this.imPrefs.PREF_STATUSWIN_ENABLED , "bool", true);
	this.initPref(this.imPrefs.PREF_REMEMBER_PASSWORD, "bool", true);
	this.initPref(this.imPrefs.PREF_AUTOLOGIN_ENABLED, "bool", false);
	this.initPref(this.imPrefs.PREF_AUTORECONNECT_ENABLED, "bool", true);
	this.initPref(this.imPrefs.PREF_SERVER_HOST, "char", "im.apinc.org");
	this.initPref(this.imPrefs.PREF_SERVER_PORT, "int", "5222");
	this.initPref(this.imPrefs.PREF_SERVER_TLS, "bool", false);
	this.initPref(this.imPrefs.PREF_SERVER_SSL, "bool", false);
	this.initPref(this.imPrefs.PREF_SERVER_RESOURCE, "char", "");
//	this.initPref(this.imPrefs.HTTP_GW, "char", "http://im.apinc.org/punjab/httpb");
	this.initPref(this.imPrefs.HTTP_UPDATE_INTERVAL, "int", 10);
	this.initPref(this.imPrefs.HTTP_BINDING, "bool", true);
	this.initPref(this.imPrefs.HTTP_REMEMBER_PASSWORD, "bool", true);
	this.initPref(this.imPrefs.PREF_NOTIFICATIONS_ENABLED, "bool", true);
	this.initPref(this.imPrefs.PREF_SOUNDNOTIFICATIONS_ENABLED, "bool", false);
	this.initPref(this.imPrefs.PREF_DEBUG_CONSOLE_ENABLED, "bool", false);	
  }
  else 
  {
	var parent=this;
    // add pref observers
	this.PrefChangeObserver = 
	{
		observe: function(aSubject, aTopic, aData)
		{
		  parent.prefChanged(aData);
		}
	}
  }
  this.imPrefs.addObserver("jazzila", this.PrefChangeObserver);
  try {
	this.nsIXmppService = CC["@mozilla.org/xmpp;1"].getService(CI.nsIXmppService);
	this.nsIXmppService.addListener(this);
	if (this.imPrefs.getBoolPref("jazzila.autologin.enabled"))
	{
		this.connect();
	}       
  } catch (e) {
	alert( "XmppService not installed: " + e);
  }
}

Jazzila.prototype.parent=Listener;
Jazzila.prototype.isConnected = false;
Jazzila.prototype.hasNewMessages = false;
Jazzila.prototype.nsIXmppService = null;
Jazzila.prototype.user = null;

Jazzila.prototype.initPref = function (aPrefName, aPrefType, aDefaultValue){
  var prefExists = null;
  switch (aPrefType) {
    case "bool" :
      prefExists = this.imPrefs.getBoolPref(aPrefName);
      if (prefExists == null)
        this.imPrefs.setBoolPref(aPrefName, aDefaultValue);
      break;

    case "int" :
      prefExists = this.imPrefs.getIntPref(aPrefName);
      if (prefExists == null)
        this.imPrefs.setIntPref(aPrefName, aDefaultValue);
      break;

    case "char" :
      prefExists = this.imPrefs.getCharPref(aPrefName);
      if (prefExists == null)
        this.imPrefs.setCharPref(aPrefName, aDefaultValue);
      break;
  }
}

Jazzila.prototype.openPreferences= function ()
{
	this.openWindow("chrome://jazzila/content/accounts.xul", "Preferences");
}

Jazzila.prototype.openPreferences= function ()
{
	this.openWindow("chrome://jazzila/content/preferences.xul", "Preferences");
}

Jazzila.prototype.setHttpGW = function(http_gw)
{
    var user = {value : ""};
    var pass = {value : ""};	
	if (this.imPrefs.getBoolPref("jazzila.http.remember.password"))
	{
		pmgr.loadLogin(http_gw, user, value);
	}
	var http_user = user.value; 
	var http_pass = pass.value;
	if (http_user && http_user != "" )
	{
		var pass=new Object();
		var check = new Object();
		if (!window.prompt.promptPassword("http", "Enter the password for http user ", pass, "remember password", check))
			return;
		http_pass = pass.value;
		if (check.checked)
		{
			this.imPrefs.setBoolPref("jazzila.http.remember.password", true);
			this.savePasswd(http_gw, http_user, http_pass);
		}
	}
	this.nsIXmppService.setHttpUser(http_user);
	this.nsIXmppService.setHttpPassword(http_pass);
}


Jazzila.prototype.connect = function ()
{
	var host = this.imPrefs.getCharPref(kHOST);
	var port = this.imPrefs.getIntPref(kPORT);
	var resource = this.imPrefs.getCharPref(kRSRC);

    var user = {value : ""};
    var pass = {value : ""};	

    var http_gw = this.imPrefs.getCharPref(this.imPrefs.HTTP_GW);
	if (http_gw)
	{	
		this.setHttpGW(http_gw);
	}	    

	pmgr.loadLogin(host, user, pass);
	if (!this.imPrefs.getBoolPref(kRMBPWD))
	{
		pass.value ="";
		if(!window.prompt.promptPassword("xmpp", "Enter the password for", pass, "remember password", check))
			return;
		if (check.checked)
		{
			this.imPrefs.setBoolPref (kRMBPWD, true);
			this.savePasswd(this.host, this.user, this.pass);
		}
	}
	this.nsIXmppService.setUser(user.value);
	this.nsIXmppService.setPassword(pass.value);

	if (!host || !user.value)
	{
		this.openWindow("chrome://jazzila/content/preferences.xul", "Preferences");
		return;
	}
	if (user.value.match(/(.*)@(.*)/))
	{
		this.user = RegExp.$1;
		this.domain = RegExp.$2;
	}
	else 
	{
		this.domain = host;
		this.user = user.value;
	}
	this.nsIXmppService.connect(host, port, resource); 
}

Jazzila.prototype.search = function (first, last, nick, email)
{
   this.nsIXmppService.search(first, last, nick, email); 
}

Jazzila.prototype.getVCard = function (jid)
{
   this.nsIXmppService.getVCard(jid); 
}

Jazzila.prototype.prefChanged = function (aPrefName){
  switch(aPrefName){
    case this.imPrefs.PREF_SERVER_HOST:
      break;

  }
}

Jazzila.prototype.showContextMenu=function(x, y, menu, ctx)
{
	var popup=document.getElementById(menu);
	var status=document.getElementById(ctx);
	if (popup)
	{
		popup.showPopup(status, x, y, "topleft", "bottomright");
	}
}

Jazzila.prototype.openWindow = function (path, name, args)
{
	var browser = CC["@mozilla.org/embedcomp/window-watcher;1"].getService(CI.nsIWindowWatcher);
	var win = browser.openWindow(null, path, name, "chrome,resizable=1,centerscreen", args);
	//win.focus();
	return win;
}


Jazzila.prototype.addListener =  function (lsr){
  	if (this.nsIXmppService)
  	{
  		this.nsIXmppService.addListener(lsr);
		this.nsIXmppService.ping();
  	}
}

Jazzila.prototype.removeListener =  function (lsr)
{
  	if (this.nsIXmppService)
  	{
		this.nsIXmppService.removeListener(lsr);
  	}
}

Jazzila.prototype.getDataSource = function ()
{
	return this.nsIXmppService.getDataSource().QueryInterface(CI.nsIRDFDataSource);
}

Jazzila.prototype.onload = function (){
  // this is false if we are in ff's customize window
//  if(document.getElementById(overlay)) {
	  window.removeEventListener("load", this.onload, false);
 /* }
  else {
	  alert("no overlay");
  }
  */
}

Jazzila.prototype.onunload = function (){
  // this is false if we are in ff's customize window
//  if(document.getElementById(overlay)){
	     // remove the listener
      jazzila.nsIXmppService.removeListener(jazzila);

    // remove the pref observer
    if (jazzila.imPrefs)
      jazzila.imPrefs.removeObserver("jazzila", jazzila.PrefChangeObserver);
//  }
  // last, but not least, clear the whole object
  jazzila = null;
}

Jazzila.prototype.onStateChange = function (state){
}

var pmgr = new PwdMgr();
var jazzila = new Jazzila();
window.addEventListener("load", Jazzila.prototype.onload, false);
window.addEventListener("unload", Jazzila.prototype.onunload, false);
